package entity

import (
	"fmt"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
	"shqsoft.cn/pas3ui/workflow"
	"shqsoft.cn/pas3ui/workflow/entity"
)

//CompZsWorkflowNewAppHis
type CompZsWorkflowNewAppHis struct {
	ID       int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	JLID     int       `gorm:"column:JLID"`
	JYRQ     time.Time `gorm:"column:JYRQ"`
	ITNO     string    `gorm:"column:ITNO"`
	ITNM     string    `gorm:"column:ITNM"`
	CUNM     string    `gorm:"column:CUNM"`
	CYNM     string    `gorm:"column:CYNM"`
	ZSJE     float64   `gorm:"column:ZSJE"`
	EMNO1    string    `gorm:"column:EMNO1"`
	EMID1    int       `gorm:"column:EMID1"`
	EMNM1    string    `gorm:"-" json:"emnm1"`
	DSVL1    float64   `gorm:"column:DSVL1"`
	EMNO2    string    `gorm:"column:EMNO2"`
	EMNM2    string    `gorm:"-" json:"emnm2"`
	EMID2    int       `gorm:"column:EMID2"`
	DSVL2    float64   `gorm:"column:DSVL2"`
	EMNO3    string    `gorm:"column:EMNO3"`
	EMNM3    string    `gorm:"-" json:"emnm3"`
	EMID3    int       `gorm:"column:EMID3"`
	DSVL3    float64   `gorm:"column:DSVL3"`
	EMNO4    string    `gorm:"column:EMNO4"`
	EMNM4    string    `gorm:"-" json:"emnm4"`
	EMID4    int       `gorm:"column:EMID4"`
	DSVL4    float64   `gorm:"column:DSVL4"`
	EMNO5    string    `gorm:"column:EMNO5"`
	EMNM5    string    `gorm:"-" json:"emnm5"`
	EMID5    int       `gorm:"column:EMID5"`
	DSVL5    float64   `gorm:"column:DSVL5"`
	EMNO6    string    `gorm:"column:EMNO6"`
	EMNM6    string    `gorm:"-" json:"emnm6"`
	EMID6    int       `gorm:"column:EMID6"`
	DSVL6    float64   `gorm:"column:DSVL6"`
	EMNO7    string    `gorm:"column:EMNO7"`
	EMNM7    string    `gorm:"-" json:"emnm7"`
	EMID7    int       `gorm:"column:EMID7"`
	DSVL7    float64   `gorm:"column:DSVL7"`
	EMNO8    string    `gorm:"column:EMNO8"`
	EMNM8    string    `gorm:"-" json:"emnm8"`
	EMID8    int       `gorm:"column:EMID8"`
	DSVL8    float64   `gorm:"column:DSVL8"`
	EMNO9    string    `gorm:"column:EMNO9"`
	EMNM9    string    `gorm:"-" json:"emnm9"`
	EMID9    int       `gorm:"column:EMID9"`
	DSVL9    float64   `gorm:"column:DSVL9"`
	EMNO10   string    `gorm:"column:EMNO10"`
	EMNM10   string    `gorm:"-" json:"emnm10"`
	EMID10   int       `gorm:"column:EMID10"`
	DSVL10   float64   `gorm:"column:DSVL10"`
	EMNO11   string    `gorm:"column:EMNO11"`
	EMNM11   string    `gorm:"-" json:"emnm11"`
	EMID11   int       `gorm:"column:EMID11"`
	DSVL11   float64   `gorm:"column:DSVL11"`
	EMNO12   string    `gorm:"column:EMNO12"`
	EMNM12   string    `gorm:"-" json:"emnm12"`
	EMID12   int       `gorm:"column:EMID12"`
	DSVL12   float64   `gorm:"column:DSVL12"`
	EMNO13   string    `gorm:"column:EMNO13"`
	EMNM13   string    `gorm:"-" json:"emnm13"`
	EMID13   int       `gorm:"column:EMID13"`
	DSVL13   float64   `gorm:"column:DSVL13"`
	EMNO14   string    `gorm:"column:EMNO14"`
	EMNM14   string    `gorm:"-" json:"emnm14"`
	EMID14   int       `gorm:"column:EMID14"`
	DSVL14   float64   `gorm:"column:DSVL14"`
	EMNO15   string    `gorm:"column:EMNO15"`
	EMNM15   string    `gorm:"-" json:"emnm15"`
	EMID15   int       `gorm:"column:EMID15"`
	DSVL15   float64   `gorm:"column:DSVL15"`
	UID      int       `gorm:"column:UID"`
	FRID     int       `gorm:"column:FRID"`
	WFID     int       `gorm:"column:WFID"`
	SQR      string    `gorm:"column:SQR"`
	SQDT     time.Time `gorm:"column:SQDT"`
	STEP     int       `gorm:"-" json:"step"`
	Progress float64   `gorm:"-" json:"progress"`
	StepName string    `gorm:"-" json:"stepName"`
	Status   string    `gorm:"-" json:"status"`
}

//TableName
func (CompZsWorkflowNewAppHis) TableName() string {
	return "T_COMP_ZSCLIE_DIVI_HIS"
}

// AfterFind 查询后触发器
func (i *CompZsWorkflowNewAppHis) AfterFind(tx *gorm.DB) (err error) {
	ec := base.GetEmployeeCache()
	i.EMNM1, err = ec.GetNameByID(i.EMID1)
	if err != nil {
		return err
	}
	if i.DSVL2 > 0 {
		i.EMNM2, err = ec.GetNameByID(i.EMID2)
		if err != nil {
			return err
		}
	}
	if i.DSVL3 > 0 {
		i.EMNM3, err = ec.GetNameByID(i.EMID3)
		if err != nil {
			return err
		}
	}
	if i.DSVL4 > 0 {
		i.EMNM4, err = ec.GetNameByID(i.EMID4)
		if err != nil {
			return err
		}
	}
	if i.DSVL5 > 0 {
		i.EMNM5, err = ec.GetNameByID(i.EMID5)
		if err != nil {
			return err
		}
	}
	if i.DSVL6 > 0 {
		i.EMNM6, err = ec.GetNameByID(i.EMID6)
		if err != nil {
			return err
		}
	}
	if i.DSVL7 > 0 {
		i.EMNM7, err = ec.GetNameByID(i.EMID7)
		if err != nil {
			return err
		}
	}
	if i.DSVL8 > 0 {
		i.EMNM8, err = ec.GetNameByID(i.EMID8)
		if err != nil {
			return err
		}
	}
	if i.DSVL9 > 0 {
		i.EMNM9, err = ec.GetNameByID(i.EMID9)
		if err != nil {
			return err
		}
	}
	if i.DSVL10 > 0 {
		i.EMNM10, err = ec.GetNameByID(i.EMID10)
		if err != nil {
			return err
		}
	}
	if i.DSVL11 > 0 {
		i.EMNM11, err = ec.GetNameByID(i.EMID11)
		if err != nil {
			return err
		}
	}
	if i.DSVL12 > 0 {
		i.EMNM12, err = ec.GetNameByID(i.EMID12)
		if err != nil {
			return err
		}
	}
	if i.DSVL13 > 0 {
		i.EMNM13, err = ec.GetNameByID(i.EMID13)
		if err != nil {
			return err
		}
	}
	if i.DSVL14 > 0 {
		i.EMNM14, err = ec.GetNameByID(i.EMID14)
		if err != nil {
			return err
		}
	}
	if i.DSVL15 > 0 {
		i.EMNM15, err = ec.GetNameByID(i.EMID15)
		if err != nil {
			return err
		}
	}
	if i.FRID == 0 {
		i.Status = "exception"
		i.StepName = "校验错误"
		return
	}
	run := entity.FlowRunHis{}
	if err = tx.Find(&run, i.FRID).Error; err != nil {
		tx.Rollback()
		return
	}
	wf := workflow.GetWorkFlow(run.WFID)
	if wf == nil {
		return fmt.Errorf("指定的流程ID:%d在流程配置中不存在！", run.WFID)
	}
	act := wf.GetAction(run.Step)
	if act == nil {
		return fmt.Errorf("指定的stepID:%d在流程%s配置中不存在！", run.Step, wf.Name)
	}
	count := len(wf.Actions)
	i.STEP = run.Step
	i.StepName = "审批中"
	if i.STEP == 1 {
		i.StepName = "申请中"
	}
	i.Progress = float64(act.Step*100) / float64(count)
	if i.Progress >= 100 && run.State == "0" {
		i.Progress = 95
	}
	i.Status = ""
	if run.State == "0" {
		i.Status = "active"
	} else if run.State == "2" {
		i.StepName = "已中止"
		i.Status = "exception"
	} else {
		i.StepName = "已完成"
		i.Status = "success"
	}
	return
}

func CompZsWorkflowNewAppHisEF() base.EntityFunc {
	return func() interface{} {
		entity := CompZsWorkflowNewAppHis{}
		return &entity
	}
}
func CompZsWorkflowNewAppHisESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CompZsWorkflowNewAppHis
		return &entities
	}
}

func init() {
	dao := base.NewDefaultDao(CompZsWorkflowNewAppHisEF(), CompZsWorkflowNewAppHisESF(), nil, "my")
	filter := base.WorkFolowFilter{
		FilterColName: "FRID",
	}
	ei, err := workflow.GetEntityInfo("CompZsWorkflowNewApp")
	if err != nil {
		panic(err)
	}
	filter.SetAdditional("wfid", ei.WfidsToString())
	filter.SetAdditional("manager", ei.MgMapToString())
	dao.Fileter = &filter
	dao.Fileter.SetDB("my")
	base.DaoPoolObj.Put("CompZsWorkflowNewAppHis", dao)
}
